/*#################################################

Tabbed Slideshow | v1.4
Max Felker | max@bigroomstudios.com
Brian Leighton | brian@bigroomstudios.com

Extends Tabs class and attaches a Slideshow class to each panel. 
Tabs change on slideshow completion.

#################################################*/

// START Class
var TabbedSlideshow = Class.create(Tabs,{

	initialize: function($super,config) {
		
		$super(config);
	
		config.duration ? this.tab_slide_duration = config.duration : this.tab_slide_duration = 5.0;
		
		this.tabs.each(function(tab) {
	
			tab.slideshow = new Slideshow({ 
				container: tab.panel, 
				duration: this.tab_slide_duration, 
				autostart: false,
				loop: false
			});
						
			tab.on('click', function() {
			
				this.stop_status_check();
				
				this.tabs.each(function(tab) {
					tab.slideshow.reset();
				}.bind(this));
				
				//start the clicked slideshow
				this.current_tab.slideshow.start();
				this.current_tab.slideshow.show_slide();
				this.start_status_check();
				
			}.bind(this));
		
		
		}.bind(this));
		
		this.current_tab.slideshow.start();
		this.current_tab.slideshow.show_slide();
		this.start_status_check();
	},
	
	slides_reset: function() {
	 
		this.tabs.each(function(tab) {
			tab.slideshow.completed = false;
			tab.slideshow.restart();
			tab.slideshow.stop();
		}.bind(this));
		
		this.current_tab.slideshow.completed = false;
		this.current_tab.slideshow.start();
		
		this.start_status_check();
	 
	},
	
	next_tab: function($super) {
		//reset the current slideshow
		this.current_tab.slideshow.reset();
		
		$super();
		
		//start the new tab
		this.current_tab.slideshow.completed = false;
		
		this.current_tab.slideshow.start();
		this.current_tab.slideshow.show_slide();
		this.start_status_check();
	},
	
	previous_tab: function($super) {
		//reset the current slideshow
		this.current_tab.slideshow.reset();
		
		$super();
		
		//start the new tab
		this.current_tab.slideshow.completed = false;
		
		this.current_tab.slideshow.start();
		this.current_tab.slideshow.show_slide();
		this.start_status_check();
	},
	
	
	start_status_check: function() {
	
		this.executer = new PeriodicalExecuter(function() {
			if(this.current_tab.slideshow.completed) {
				this.has_looped = false;
				if(this.tab_current==this.tabs_count-1) {
					this.has_looped = true;
				} 
				this.current_tab.slideshow.completed = false;
				this.next_tab();
				this.stop_status_check();
			}
		}.bind(this),this.tab_slide_duration);
			
	},
	
	stop_status_check: function() {
	
		if(this.executer) {
			this.executer.stop();
			this.executer = '';
		} else {
			return false;
		}
	
	}

});
